package kubayaxi.video.service.base.util.sm;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.crypto.symmetric.SM4;

import java.util.Objects;

/**
 * @description 国密工具
 */
public class SmartUtil {
    public static  final SM4 sm4 = SmartUtil.getSM4(SmartConstant.SM4_DEFAULT_KEY);
//    private static final SM2Engine.Mode DEFAULT_SM2_MODE = SM2Engine.Mode.C1C3C2;




    private SmartUtil() {
    }
    /**
     * sm2 私钥解密
     */
//    public static String sm2PrivateKeyDecrypt(String privateKey, String encryptContent) {
//        if (StrUtil.isBlank(encryptContent)) {
//            return encryptContent;
//        }
//        SM2 sm2 = SmUtil.sm2(privateKey, null).setMode(DEFAULT_SM2_MODE);
//        // 私钥解密
//        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptContent, KeyType.PrivateKey));
//        return decryptStr;
//    }

    /**
     * sm4 加密
     *
     * @param key
     * @param content
     * @return
     */
    public static String sm4Encrypt(String key, String content) {
        return sm4Encrypt(getSM4(key), content);
    }


    /**
     * sm4 加密
     *
     * @param sm4
     * @param content
     * @return
     */
    public static String sm4Encrypt(SM4 sm4, String content) {
        Objects.requireNonNull(sm4);
        try {
            return sm4.encryptBase64(content);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * sm4 解密
     *
     * @param key
     * @param content
     * @return
     */
    public static String sm4Decrypt(String key, String content) {
        return sm4Decrypt(getSM4(key), content);
    }

    /**
     * sm4 解密
     *
     * @param sm4
     * @param content
     * @return
     */
    public static String sm4Decrypt(SM4 sm4, String content) {
        Objects.requireNonNull(sm4);
        try {
            return sm4.decryptStr(content);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取 sm4对象
     *
     * @param key
     * @return
     */
    public static SM4 getSM4(String key) {
        return SmUtil.sm4(sm4KeyHandle(StrUtil.bytes(key)));
    }

    /**
     * key 处理
     *
     * @param bytes
     * @return
     */
    private static byte[] sm4KeyHandle(byte[] bytes) {
        Objects.requireNonNull(bytes, "sm4对应key不能为空");
        final int keyLen = 16;
        byte[] keyByte = new byte[keyLen];
        int lastIndex = bytes.length - 1;
        for (int i = 0; i < keyLen; i++) {
            int index = i > lastIndex ? lastIndex : i;
            keyByte[i] = bytes[index];
        }
        return keyByte;
    }

    /**
     * HMACMD5 摘要加密(自带key)
     *
     * @param key
     * @param content
     * @return
     */
    public static String hamacMD5Encrypt(String key, String content) {
        if (StrUtil.isBlank(content)) {
            return content;
        }
        if (StrUtil.isBlank(key)) {
            key = SmartConstant.HMAC_DEFAULT_KEY;
        }

        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key.getBytes());
        return mac.digestHex(content);
    }

    /**
     * HMACMD5 摘要加密
     *
     * @param content
     * @return
     */
    public static String hamacMD5Encrypt(String content) {
        if (StrUtil.isBlank(content)) {
            return content;
        }
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, SmartConstant.HMAC_DEFAULT_KEY.getBytes());
        return mac.digestHex(content);
    }

    public static String sha256Encrypt(String content) {
        if (StrUtil.isBlank(content)) {
            return content;
        }
        Digester digester = SecureUtil.sha256();
        return digester.digestHex(content);
    }
}
